﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using AutoMapper;
using System.Data;

namespace Data
{
   public class KhachHangRepository
    {
       private static KhachHangDTO MapKhachHangEFToKhachHangDTO(KHACHHANG khachhangEF)
       {
           Mapper.CreateMap<KHACHHANG, KhachHangDTO>();
           return Mapper.Map<KHACHHANG, KhachHangDTO>(khachhangEF);
       }
       private static KHACHHANG MapKhachHangDTOToKhachHangEF(KhachHangDTO khachhangDTO)
       {
           Mapper.CreateMap<KhachHangDTO, KHACHHANG>();
           return Mapper.Map<KhachHangDTO, KHACHHANG>(khachhangDTO);
       }
       public static KhachHangDTO GetKhachHangByID(int makhachhang)
       {
           KhachHangDTO khachhangDTO = new KhachHangDTO();
           using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
           {
               KHACHHANG query = (from khachhang in context.KHACHHANGList
                                 where khachhang.MaKhachHang == makhachhang
                                 select khachhang).First();
               khachhangDTO = MapKhachHangEFToKhachHangDTO(query);

           }
           return khachhangDTO;
       }
       public static List<KhachHangDTO> GetAllKhachHang()
       {
           List<KhachHangDTO> list=new List<KhachHangDTO>();
           using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
           {
               try
               {
                   var query = from khachhang in context.KHACHHANGList
                               select khachhang;
                   foreach (var khachhangEF in query)
                   {
                       KhachHangDTO khachhangDTO = MapKhachHangEFToKhachHangDTO(khachhangEF);
                       list.Add(khachhangDTO);
                   }
               }
               catch
               {

               }
           }
           return list;
       }
       public void Insert(KhachHangDTO khachangDTO)
       {
           using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
           {
               try
               {
                   KHACHHANG khachhangEF = new KHACHHANG();
                   khachhangEF = MapKhachHangDTOToKhachHangEF(khachangDTO);
                   context.AddToKHACHHANGList(khachhangEF);
                   context.SaveChanges();
               }
               catch
               {

               }
           }
       }
       public void Update(KhachHangDTO khachhangDTO)
       {
           using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
           {
               try
               {
                   EntityKey key = context.CreateEntityKey("KHACHHANGList", new KHACHHANG { MaKhachHang = khachhangDTO.MaKhachHang });
                   KHACHHANG origkhachhang = context.GetObjectByKey(key) as KHACHHANG;
                   context.Detach(origkhachhang);
                   context.Attach(origkhachhang);
                   KHACHHANG updatekhachhang = MapKhachHangDTOToKhachHangEF(khachhangDTO);
                   context.ApplyCurrentValues<KHACHHANG>("KHACHHANGList", updatekhachhang);
                   context.SaveChanges();

               }
               catch (Exception)
               {
               }
           }
       }
       public static bool KiemTraTonTaiMaKhachHang(int makhachhang)
       {
           bool kq = false; ;
           using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
           {
               var query = from khachhang in context.KHACHHANGList
                           select khachhang.MaKhachHang;
               List<int> list = query.ToList<int>();
               foreach (int i in list)
               {
                   if (i ==makhachhang)
                       kq = true;
               }
           }
           return kq;
       }
       
    }
}
